<?php
require_once ("views/LoginView.php");

class LoginController {
	private $m_loginModel;
	private $m_loginViewer;

	/**
	 * Constructor, creates our private loginHandler- and loginViewer- objects from injected params
	 * @param LoginHandler $loginHandler, LoginView $loginViewer
	 */
	public function __construct(LoginModel $a_loginModel) {
		$this -> m_loginModel = $a_loginModel;
		$this -> m_loginViewer = new loginView();
	}

	public function doControll() {

		//Tried to logout?
		if ($this -> m_loginViewer -> triedToLogOut() === true) {
			$this -> m_loginModel -> logOut();
		}

		//Tried to login?
		if ($this -> m_loginViewer -> triedToLogIn() === true) {
			$this -> m_loginModel -> logIn($this -> m_loginViewer -> getUserName(), $this -> m_loginViewer -> getPassword());
		}

		//Handle output (logged in / logged out)
		if ($this -> m_loginModel -> isUserLoggedIn() === true) {
			return $this -> m_loginViewer -> generateLogoutButton();
		} else {
			return $this -> m_loginViewer -> generateLoginBox();
		}
	}
    
    public function isUserLoggedIn()  {
        return $this->m_loginModel->isUserLoggedIn() ;
    }

}
